/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DaoExtendido;

import Logico.Disciplina;
import java.util.ArrayList;
import ConectorBD.ConexionBD;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import Dao.DisciplinaJpaController;
import javax.persistence.EntityManagerFactory;


public class DisciplinaExtension extends DisciplinaJpaController {
    ConexionBD conexion;
    Connection con;

    public DisciplinaExtension(EntityManagerFactory emf) {

        super(emf);
        conexion = new ConexionBD();
        this.emf = emf;
    }

    public ArrayList<Disciplina> buscarPorNombre(String nombre) {

        boolean flag = false;
        String consulta = "SELECT * FROM disciplina WHERE nombre = " + nombre + " order by iddisciplina;";
        con = conexion.conectar();

        ArrayList<Disciplina> resultado = new ArrayList<>();

        Statement stmt;
        ResultSet rs;

        try {

            stmt = con.createStatement();
            rs = stmt.executeQuery(consulta);

            while (rs.next()) {
                Disciplina temp = new Disciplina();
                
                temp.setIddisciplina(Integer.parseInt(rs.getObject(1) + ""));
                temp.setNombre(rs.getObject(2) + "");
                temp.setDescripcion(rs.getObject(3) + "");
                temp.setLogo(rs.getObject(4) + "");

                resultado.add(temp);
                flag = true;
            }

            if (flag == false) {
                resultado.add(new Disciplina());
            }

            rs.close();
            con.close();

        } catch (SQLException exp) {
            JOptionPane.showMessageDialog(null, exp.getMessage());
        }

        return resultado;

    }
}
